//
//  MyViewController.m
//
//  Created by nf on 17/12/10.
//  Copyright © 2018年 NF. All rights reserved.
//

#import "MyViewController.h"
#import "UserInfoViewController.h"
#import "AboutPlatformController.h"
#import "FriendsCodeController.h"
#import "AboutUsController.h"
#import "YejiViewController.h"
#import "LoginViewController.h"
#import "MeViewCell.h"
#import "MyInfoModel.h"
#import "MyPageModel.h"


@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate,BaseModelProtocol>
{
    BOOL _logoutNoti ;
}

@property (nonatomic,strong) UIView       *infoView ;
@property (nonatomic,strong) UIImageView  *headerIconImg ;
@property (nonatomic,strong) UILabel      *nickNameLabel ;
@property (nonatomic,strong) UILabel      *phoneLabel ;
@property (nonatomic,strong) UILabel      *numberLabel ;
@property (nonatomic,strong) UILabel      *loginLabel ;
@property (nonatomic,strong) UITableView  *tableView ;
@property (nonatomic,strong) MyInfoModel *infoModel;
@property (nonatomic,strong) MyPageModel *pageModel;
@property (nonatomic,strong) NSMutableArray *pageArray;

@end

static const CGFloat TABLEHEADVIEW_HEIGHT = 180.0f ;

@implementation MyViewController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    [self.infoModel getMyInfoData];
}

- (void)initView
{
    [super initView];
    
    [self.view addSubview:self.statusOrangeView];
    [self.view addSubview:self.infoView];
    [self.view addSubview:self.tableView];
    
    [self.pageModel getMyPageData];
}

- (void)getDataFinish:(BaseModel *)baseModel netResponse:(BaseResponse *)response{
    if ([baseModel isKindOfClass:[MyInfoModel class]])
    {
        EFAppContext *appContext = [EFAppContext sharedContext];
        if(!ISEMPTY(appContext.authorization)){
            MyInfoResponse *resp = (MyInfoResponse *)response;
            MyInfoData *data = resp.resObject;
            [_headerIconImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.headUrl]] placeholderImage:kDefaultImage];
            _nickNameLabel.text = data.name;
            _phoneLabel.text = data.mobile;
            _numberLabel.text = [NSString stringWithFormat:@"工号：%@",data.eid];
            
            EFAppContext *appc = [EFAppContext sharedContext];
            appc.userName = data.name;
            appc.headUrl = data.headUrl;
            [appc save];
        }else{
            [_loginLabel setHidden:NO];
        }
    }
    else if ([baseModel isKindOfClass:[MyPageModel class]])
    {
        MyPageResponse *resp = (MyPageResponse *)response;
        NSArray *funcList = resp.resObject.funcList;
        for (NSArray *arr in funcList) {
            [self.pageArray addObjectsFromArray:arr];
        }
        [self.tableView reloadData];

    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50.0f ;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.pageArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeViewCell" forIndexPath:indexPath];
    NSDictionary *dic = self.pageArray[indexPath.row];
    [cell buildUIWithTitle:dic[@"showName"]];
    
    UIView *view_bg = [[UIView alloc]initWithFrame:cell.frame];
    view_bg.backgroundColor = [UIColor whiteColor];
    cell.selectedBackgroundView = view_bg;
    
    return cell ;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
    NSDictionary *dic = self.pageArray[indexPath.row];
    NSString *route = dic[@"route"];
    if(ISEqual(route, @"/personalCenter/aboutplatform")){
        //关于平台
        AboutPlatformController *aboutController = [[AboutPlatformController alloc]init];
        [self.navigationController pushViewController:aboutController animated:YES];
    }
    else if(ISEqual(route, @"/personalCenter/aboutus")){
        //关于我们
        AboutUsController *aboutController = [[AboutUsController alloc]init];
        [self.navigationController pushViewController:aboutController animated:YES];
    }else if(ISEqual(route, @"/personalCenter/fenxiaocenter")){
        //分享
        FriendsCodeController *friendsController = [[FriendsCodeController alloc]init];
        [self.navigationController pushViewController:friendsController animated:YES];
    }
    else if(ISEqual(route, @"/personalCenter/myachievement")){
        //我的业绩
        YejiViewController *yejiVC = [[YejiViewController alloc]init];
        [self.navigationController pushViewController:yejiVC animated:YES];
    }
        
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
}


- (void)gotoUserInfo{
    EFAppContext *appContext = [EFAppContext sharedContext];
    if(!ISEMPTY(appContext.authorization)){
        UserInfoViewController *infoVC = [[UserInfoViewController alloc]init];
        [self.navigationController pushViewController:infoVC animated:YES];
    }else{
        LoginViewController *loginVC = [[LoginViewController alloc]init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}

- (UIView *)infoView
{
    if (!_infoView)
    {
        _infoView = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.statusOrangeView.height, SCREEN_WIDTH, 120)];
        _infoView.userInteractionEnabled = YES;
        [_infoView addTapCallBack:self sel:@selector(gotoUserInfo)];
        _infoView.backgroundColor = [UIColor whiteColor];

        _headerIconImg = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 80, 80)];
        _headerIconImg.layer.borderWidth = 2;
        _headerIconImg.layer.borderColor = kColorDefultEdge.CGColor;
        [_headerIconImg setCornerDefaultHalf];
        [_infoView addSubview:_headerIconImg];

        _nickNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(_headerIconImg.right+20, _headerIconImg.top, SCREEN_WIDTH-_headerIconImg.right-50, 25)];
        _nickNameLabel.textColor = kTitleColor_midden;
        _nickNameLabel.text = [EFAppContext sharedContext].nick;
        _nickNameLabel.font = kFontMidden;
        [_infoView addSubview:_nickNameLabel];

        _phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(_nickNameLabel.left, _nickNameLabel.bottom+5, _nickNameLabel.width, 20)];
        _phoneLabel.font = kFontMimi;
        _phoneLabel.textColor = kTitleColor_midden;
        [_infoView addSubview:_phoneLabel];
        
        _numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(_phoneLabel.left, _phoneLabel.bottom+5, _phoneLabel.width, 20)];
        _numberLabel.font = kFontMimi;
        _numberLabel.textColor = kTitleColor_midden;
        [_infoView addSubview:_numberLabel];
        
        _loginLabel = [[UILabel alloc]initWithFrame:CGRectMake(_headerIconImg.right+20, _headerIconImg.top, 100, _headerIconImg.height)];
        _loginLabel.text = @"点击登录";
        _loginLabel.font = kFontVeryBig;
        _loginLabel.textColor = kTitleColor_black;
        [_loginLabel setHidden:YES];
        [_infoView addSubview:_loginLabel];
        
        UIImageView *arrorImgView = [[UIImageView alloc]initWithFrame:CGRectMake(_infoView.width-25, 50, 6, 12)];
        arrorImgView.image = kImage(@"arrow");
        [_infoView addSubview:arrorImgView];

    }

    return _infoView;
}


- (UITableView *)tableView
{
    if (nil == _tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _infoView.bottom+10, SCREEN_WIDTH, SCREEN_HEIGHT-_infoView.bottom-49) style:UITableViewStylePlain];
        _tableView.delegate = self ;
        _tableView.dataSource = self ;
        _tableView.backgroundColor = kColorDefultBg ;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MeViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MeViewCell class])];
    }
    return _tableView ;
}

- (MyInfoModel *)infoModel{

    if (nil == _infoModel) {
        _infoModel = [[MyInfoModel alloc]initWithDelegate:self cache:NO];
    }
    return _infoModel;
}

- (MyPageModel *)pageModel{

    if (nil == _pageModel) {
        _pageModel = [[MyPageModel alloc]initWithDelegate:self cache:NO];
    }
    return _pageModel;
}

- (NSMutableArray *)pageArray
{
    if (!_pageArray)
    {
        _pageArray = [NSMutableArray array];
    }
    return _pageArray;
}

@end























